home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / admin / dig-2.0 / dig-2 / dig.2.0 / subr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-09-04  |  10.6 KB  |  479 lines

  1.  
  2. /*
  3.  * Copyright (c) 1985 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms are permitted
  7.  * provided that this notice is preserved and that due credit is given
  8.  * to the University of California at Berkeley. The name of the University
  9.  * may not be used to endorse or promote products derived from this
  10.  * software without specific prior written permission. This software
  11.  * is provided ``as is'' without express or implied warranty.
  12.  */
  13.  
  14. /*
  15. ** Modified for and distributed with 'dig' version 2.0 from
  16. ** University of Southern California Information Sciences Institute
  17. ** (USC-ISI). 9/1/90
  18. */
  19.  
  20. #ifndef lint
  21. static char sccsid[] = "@(#)subr.c    5.9 (Berkeley) 2/17/88";
  22. #endif /* not lint */
  23.  
  24. #include "hfiles.h"
  25.  
  26. /*
  27.  ****************************************************************************
  28.  *
  29.  *  subr.c --
  30.  *
  31.  *    Miscellaneous subroutines for the name server 
  32.  *    lookup program.
  33.  *  
  34.  *    Copyright (c) 1985 
  35.  *      Andrew Cherenson
  36.  *      CS298-26  Fall 1985
  37.  *
  38.  ****************************************************************************
  39. */
  40.  
  41. #include <stdio.h>
  42. #include <strings.h>
  43. #include <sys/types.h>
  44. #include NETDBH
  45. #include <sys/socket.h>
  46. #include NAMESERH
  47. #ifndef T_TXT
  48. #define T_TXT 16
  49. #endif T_TXT
  50. #include <signal.h>
  51. #include <setjmp.h>
  52. #include "res.h"
  53.  
  54.  
  55. /*
  56.  *****************************************************************************
  57.  *
  58.  *  DecodeError --
  59.  *
  60.  *    Converts an error code into a character string.
  61.  *
  62.  ****************************************************************************
  63. */
  64.  
  65.  
  66. /*
  67. char *
  68. DecodeError(result)
  69.     int result;
  70. {
  71.     switch(result) {
  72.         case NOERROR:     return("Success"); break;
  73.         case FORMERR:    return("Format error"); break;
  74.         case SERVFAIL:    return("Server failed"); break;
  75.         case NXDOMAIN:    return("Non-existent domain"); break;
  76.         case NOTIMP:    return("Not implemented"); break;
  77.         case REFUSED:    return("Query refused"); break;
  78.         case NOCHANGE:    return("No change"); break;
  79.         case NO_INFO:     return("No information"); break;
  80.         case ERROR:     return("Unspecified error"); break;
  81.         case TIME_OUT:     return("Timed out"); break;
  82.         case NONAUTH:     return("Non-authoritative answer"); break;
  83.         default:         break;
  84.     }
  85.     return("BAD ERROR VALUE"); 
  86.       }
  87.  
  88.  
  89. */
  90.  
  91.  
  92.  
  93. int
  94. StringToClass(class, dflt)
  95.     char *class;
  96.     int dflt;
  97. {
  98.     if (strcasecmp(class, "IN") == 0)
  99.         return(C_IN);
  100.     if (strcasecmp(class, "CHAOS") == 0)
  101.         return(C_CHAOS);
  102.     if (strcasecmp(class, "ANY") == 0)
  103.         return(C_ANY);
  104. /*    fprintf(stderr, "; ** unknown query class: %s\n", class); */
  105.     return(dflt);
  106. }
  107.  
  108. /*
  109.  ***************************************************************************
  110.  *
  111.  *  StringToType --
  112.  *
  113.  *    Converts a string form of a query type name to its 
  114.  *    corresponding integer value.
  115.  *
  116.  *******************************************************************************
  117.  */
  118.  
  119.  
  120. int
  121. StringToType(type, dflt)
  122.     char *type;
  123.     int dflt;
  124. {
  125.     if (strcasecmp(type, "A") == 0)
  126.         return(T_A);
  127.     if (strcasecmp(type, "NS") == 0)
  128.         return(T_NS);            /* authoritative server */
  129.     if (strcasecmp(type, "MX") == 0)
  130.         return(T_MX);            /* mail exchanger */
  131.     if (strcasecmp(type, "CNAME") == 0)
  132.         return(T_CNAME);        /* canonical name */
  133.     if (strcasecmp(type, "SOA") == 0)
  134.         return(T_SOA);            /* start of authority zone */
  135.     if (strcasecmp(type, "MB") == 0)
  136.         return(T_MB);            /* mailbox domain name */
  137.     if (strcasecmp(type, "MG") == 0)
  138.         return(T_MG);            /* mail group member */
  139.     if (strcasecmp(type, "MR") == 0)
  140.         return(T_MR);            /* mail rename name */
  141.     if (strcasecmp(type, "WKS") == 0)
  142.         return(T_WKS);            /* well known service */
  143.     if (strcasecmp(type, "PTR") == 0)
  144.         return(T_PTR);            /* domain name pointer */
  145.     if (strcasecmp(type, "HINFO") == 0)
  146.         return(T_HINFO);        /* host information */
  147.     if (strcasecmp(type, "MINFO") == 0)
  148.         return(T_MINFO);        /* mailbox information */
  149.     if (strcasecmp(type, "AXFR") == 0)
  150.         return(T_AXFR);            /* zone transfer */
  151.     if (strcasecmp(type, "MAILB") == 0)
  152.         return(T_MAILB);        /* mail box */
  153.     if (strcasecmp(type, "ANY") == 0)
  154.         return(T_ANY);            /* matches any type */
  155.     if (strcasecmp(type, "TXT") == 0)       
  156.       return(T_TXT);                        /* text strings */
  157.     if (strcasecmp(type, "UINFO") == 0)
  158.         return(T_UINFO);        /* user info */
  159.     if (strcasecmp(type, "UID") == 0)
  160.         return(T_UID);            /* user id */
  161.     if (strcasecmp(type, "GID") == 0)
  162.         return(T_GID);            /* group id */
  163. /*    fprintf(stderr, "; ** unknown query type: %s\n", type); */
  164.     return(dflt);
  165. }
  166.  
  167. /*
  168.  ****************************************************************************
  169.  *
  170.  *  DecodeType --
  171.  *
  172.  *    Converts a query type to a descriptive name.
  173.  *    (A more verbose form of p_type.)
  174.  *
  175.  *
  176.  ****************************************************************************
  177. */
  178.  
  179. static  char nbuf[20];
  180.  
  181. char *
  182. DecodeType(type)
  183.     int type;
  184. {
  185.     switch (type) {
  186.     case T_A:
  187.         return("address");
  188.     case T_NS:
  189.         return("name server");
  190.     case T_MX:        
  191.         return("mail exchanger");
  192.     case T_CNAME:        
  193.         return("cannonical name");
  194.     case T_SOA:        
  195.         return("start of authority zone");
  196.     case T_MB:        
  197.         return("mailbox domain name");
  198.     case T_MG:        
  199.         return("mail group member");
  200.     case T_MR:        
  201.         return("mail rename name");
  202.     case T_NULL:        
  203.         return("null resource record");
  204.     case T_WKS:        
  205.         return("well known service");
  206.     case T_PTR:        
  207.         return("domain name pointer");
  208.     case T_HINFO:        
  209.         return("host");
  210.     case T_MINFO:        
  211.         return("mailbox (MINFO)");
  212.     case T_AXFR:        
  213.         return("zone transfer");
  214.     case T_MAILB:        
  215.         return("mail box");
  216.     case T_ANY:        
  217.         return("any type");
  218.     case T_UINFO:
  219.         return("user info");
  220.     case T_UID:
  221.         return("user id");
  222.     case T_GID:
  223.         return("group id");
  224.         case T_TXT:
  225.         return("txt");
  226.     default:
  227.         (void) sprintf(nbuf, "%d", type);
  228.         return (nbuf);
  229.     }
  230. }
  231.  
  232.  
  233. /*
  234. ******************************************************************************
  235.  *
  236.  *  IntrHandler --
  237.  *
  238.  *    This routine is called whenever a control-C is typed. 
  239.  *    It performs three main functions:
  240.  *     - close an open socket connection.
  241.  *     - close an open output file (used by LookupHost, et al.)
  242.  *     - jump back to the main read-eval loop.
  243.  *        
  244.  *    Since a user may type a ^C in the middle of a routine that
  245.  *    is using a socket, the socket would never get closed by the 
  246.  *    routine. To prevent an overflow of the process's open file table,
  247.  *    the socket and output file descriptors are closed by
  248.  *    the interrupt handler.
  249.  *
  250.  *  Side effects:
  251.  *    If sockFD is valid, its socket is closed.
  252.  *    If filePtr is valid, its file is closed.
  253.  *    Flow of control returns to the main() routine.
  254.  *
  255.  ****************************************************************************
  256. */
  257.  
  258. /*
  259.  
  260. int
  261. IntrHandler()
  262. {
  263.     extern jmp_buf env;
  264.  
  265.     if (sockFD >= 0) {
  266.     close(sockFD);
  267.     sockFD = -1;
  268.     }
  269.     if (filePtr != NULL && filePtr != stdout) {
  270.     fclose(filePtr);
  271.     filePtr = NULL;
  272.     }
  273.     printf("\n");
  274.     longjmp(env, 1);
  275. }
  276. */
  277.  
  278. /*
  279.  **************************************************************************
  280.  *
  281.  *  Calloc --
  282.  *
  283.  *      Calls the calloc library routine with the interrupt
  284.  *      signal blocked.  The interrupt signal blocking is done
  285.  *      to prevent malloc from getting confused if a
  286.  *      control-C arrives in the middle of its bookkeeping
  287.  *      routines.  We need to do this because a control-C
  288.  *      causes a return to the main command loop instead
  289.  *      causing the program to die.
  290.  *
  291.  *    This method doesn't prevent the pointer returned
  292.  *    by calloc from getting lost, so it is possible
  293.  *    to get "core leaks".
  294.  *
  295.  *  Results:
  296.  *    (address)    - address of new buffer.
  297.  *    NULL        - calloc failed.
  298.  *
  299.  **************************************************************************
  300. */
  301.  
  302. /*
  303. char *
  304. Calloc(num, size)
  305.     unsigned num, size;
  306. {
  307.     char     *ptr;
  308.     int     saveMask;
  309.     extern char *calloc();
  310.  
  311.     saveMask = sigblock(1 << (SIGINT-1));
  312.     ptr = calloc(num, size);
  313.     (void) sigsetmask(saveMask);
  314.     if (ptr == NULL) {
  315.         fflush(stdout);
  316.         fprintf(stderr, "; ** Calloc failed\n");
  317.         fflush(stderr);
  318.         abort();
  319.     } else {
  320.         return(ptr);
  321.     }
  322. }
  323. */
  324.  
  325. /*
  326.  ****************************************************************************
  327.  *
  328.  *  PrintHostInfo --
  329.  *
  330.  *    Prints out the HostInfo structure for a host.
  331.  *
  332.  *****************************************************************************
  333. */
  334.  
  335.  
  336. /*
  337. void
  338. PrintHostInfo(file, title, hp)
  339.     FILE     *file;
  340.     char     *title;
  341.     register HostInfo *hp;
  342. {
  343.     register char         **cp;
  344.     register ServerInfo     **sp;
  345.     char             comma;
  346.     int              i;
  347.  
  348.     fprintf(file, "%-7s  %s\n", title, hp->name);
  349.  
  350.     if (hp->addrList != NULL) {
  351.         if (hp->addrList[1] != NULL) {
  352.         fprintf(file, "Addresses:");
  353.         } else {
  354.         fprintf(file, "Address:");
  355.         }
  356.         comma = ' ';
  357.         i = 0;
  358.         for (cp = hp->addrList; cp && *cp; cp++) {
  359.         i++;
  360.         if (i > 4) {
  361.             fprintf(file, "\n\t");
  362.             comma = ' ';
  363.             i = 0;
  364.         }
  365.        fprintf(file,"%c %s", comma, inet_ntoa(*(struct in_addr *)*cp));
  366.         comma = ',';
  367.         }
  368.     }
  369.  
  370.     if (hp->aliases != NULL) {
  371.         fprintf(file, "\nAliases:");
  372.         comma = ' ';
  373.         i = 10;
  374.         for (cp = hp->aliases; cp && *cp && **cp; cp++) {
  375.         i += strlen(*cp) + 2;
  376.         if (i > 75) {
  377.             fprintf(file, "\n\t");
  378.             comma = ' ';
  379.             i = 10;
  380.         }
  381.         fprintf(file, "%c %s", comma, *cp);
  382.         comma = ',';
  383.         }
  384.     }
  385.  
  386.     if (hp->servers != NULL) {
  387.         fprintf(file, "Served by:\n");
  388.         for (sp = hp->servers; *sp != NULL ; sp++) {
  389.  
  390.         fprintf(file, "- %s\n\t",  (*sp)->name);
  391.  
  392.         comma = ' ';
  393.         i = 0;
  394.         for (cp = (*sp)->addrList; cp && *cp && **cp; cp++) {
  395.             i++;
  396.             if (i > 4) {
  397.             fprintf(file, "\n\t");
  398.             comma = ' ';
  399.             i = 0;
  400.             }
  401.             fprintf(file, 
  402.             "%c %s", comma, inet_ntoa(*(struct in_addr *)*cp));
  403.             comma = ',';
  404.         }
  405.         fprintf(file, "\n\t");
  406.  
  407.         comma = ' ';
  408.         i = 10;
  409.         for (cp = (*sp)->domains; cp && *cp && **cp; cp++) {
  410.             i += strlen(*cp) + 2;
  411.             if (i > 75) {
  412.             fprintf(file, "\n\t");
  413.             comma = ' ';
  414.             i = 10;
  415.             }
  416.             fprintf(file, "%c %s", comma, *cp);
  417.             comma = ',';
  418.         }
  419.         fprintf(file, "\n");
  420.         }
  421.     }
  422.  
  423.     fprintf(file, "\n\n");
  424. }
  425. */
  426.  
  427. /*
  428.  *****************************************************************************
  429.  *
  430.  *  OpenFile --
  431.  *
  432.  *    Parses a command string for a file name and opens
  433.  *    the file.
  434.  *
  435.  *  Results:
  436.  *    file pointer    - the open was successful.
  437.  *    NULL        - there was an error opening the file or
  438.  *              the input string was invalid.
  439.  *
  440.  *****************************************************************************
  441. */
  442.  
  443.  
  444.  
  445. /*
  446.  *  Open an output file if we see '>' or >>'.
  447.  *  Check for overwrite (">") or concatenation (">>").
  448.  */
  449.  
  450. /*
  451. FILE *
  452. OpenFile(string, file)
  453.     char *string;
  454.     char *file;
  455. {
  456.     char     *redirect;
  457.     FILE     *tmpPtr;
  458.  
  459.     redirect = index(string, '>');
  460.     if (redirect == NULL) {
  461.         return(NULL);
  462.     }
  463.     if (redirect[1] == '>') {
  464.         sscanf(redirect, ">> %s", file);
  465.         tmpPtr = fopen(file, "a+");
  466.     } else {
  467.         sscanf(redirect, "> %s", file);
  468.         tmpPtr = fopen(file, "w");
  469.     }
  470.  
  471.     if (tmpPtr != NULL) {
  472.         redirect[0] = '\0';
  473.     }
  474.  
  475.     return(tmpPtr);
  476. }
  477. */
  478.  
  479.